#include "Button.h"

volatile uint8_t BUTTON_ZHONG_S = 1;
volatile uint8_t BUTTON_LEFT_S = 1;
volatile uint8_t BUTTON_RIGHT_S = 1;

extern volatile uint8_t pageindex;

void Button_init(void)
{
    /* 配置引脚 */
    GPIO_InitTypeDef GPIO_InitStruct;
    __HAL_RCC_GPIOF_CLK_ENABLE(); /* 使能GPIOA时钟 */

    GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; /* GPIO模式为下降沿中断 */
    GPIO_InitStruct.Pull = GPIO_PULLUP;                 /* 上拉 */
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;       /* 速度为高速 */
    GPIO_InitStruct.Pin = BUTTON_ZHONG_Pin | BUTTON_LEFT_Pin;
    HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);

    __HAL_RCC_GPIOB_CLK_ENABLE(); /* 使能GPIOB时钟 */
    GPIO_InitStruct.Pin = BUTTON_RIGHT_Pin;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* 配置中断优先级 */
    HAL_NVIC_SetPriority(EXTI0_1_IRQn, 19, 0);
    HAL_NVIC_SetPriority(EXTI4_15_IRQn, 20, 0);
    /* 使能EXTI中断 */
    HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
    HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}

// #define bt4 PB_6 右
void EXTI4_15_IRQHandler(void)
{
    pageindex++;
    BUTTON_RIGHT_S = HAL_GPIO_ReadPin(BUTTON_RIGHT_GPIO_Port, BUTTON_RIGHT_Pin);
    HAL_GPIO_EXTI_IRQHandler(BUTTON_RIGHT_Pin);
}

// #define bt1 PF_1 左
// #define bt2 PF_0 中
void EXTI0_1_IRQHandler(void)
{
    if(pageindex>0){
        pageindex--;
    }
    BUTTON_ZHONG_S = HAL_GPIO_ReadPin(BUTTON_ZHONG_GPIO_Port, BUTTON_ZHONG_Pin);
    BUTTON_LEFT_S = HAL_GPIO_ReadPin(BUTTON_LEFT_GPIO_Port, BUTTON_LEFT_Pin);
    HAL_GPIO_EXTI_IRQHandler(BUTTON_ZHONG_Pin | BUTTON_LEFT_Pin);
}
